// pagesDoctor/autonomousScheduling/index.js
import {
  ApiScheduleInfo,
  ApiDelSchedule
} from '../../api/docIndex'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    dataList: [{
      date: "2025-02-13",
      d: "13",
      week: "四",
      status: "无号"
    }, {
      date: "2025-02-13",
      d: "13",
      week: "四",
      status: "无号"
    }, {
      date: "2025-02-13",
      d: "13",
      week: "四",
      status: "无号"
    }, {
      date: "2025-02-13",
      d: "13",
      week: "四",
      status: "无号"
    }, {
      date: "2025-02-13",
      d: "13",
      week: "四",
      status: "无号"
    }, {
      date: "2025-02-13",
      d: "13",
      week: "四",
      status: "无号"
    }, {
      date: "2025-02-13",
      d: "13",
      week: "四",
      status: "无号"
    }, {
      date: "2025-02-13",
      d: "13",
      week: "四",
      status: "无号"
    }, {
      date: "2025-02-13",
      d: "13",
      week: "四",
      status: "无号"
    }, {
      date: "2025-02-13",
      d: "13",
      week: "四",
      status: "无号"
    }],
    minDate: new Date().getTime(),
    date: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad() {

  },

  onShow(options) {
    let date = this.getCurrentDate()
    this.setData({
      date
    })
    this.handleSelect('', this.data.date)
  },


  getCurrentDate() {
    let today = new Date();
    let year = today.getFullYear();
    let month = (today.getMonth() + 1).toString().padStart(2, '0'); // 月份从0开始，所以加1
    let day = today.getDate().toString().padStart(2, '0'); // 保证日期是两位数
    return `${year}-${month}-${day}`;
  },
  delSchedule(e) {
    let id = e.currentTarget.dataset.v.id
    let that = this
    wx.showModal({
      title: "是否确认删除该数据？",
      success(res) {
        if (res.confirm) {
          ApiDelSchedule(id).then((res) => {
            if (res.code == '200') {
              wx.showToast({
                title: "删除成功",
              })
              that.handleSelect('', that.data.date)
            }
          })
        }

      }
    })





  },

  editSchedule(e) {
    /*     date={"id":56,"doctor_id":1,"date":"2025-02-14","schedule":"15:16-16:16","schedule_number":10,"act_number":0,"created_at":"2025-02-14 15:16:55","updated_at":"2025-02-14 15:16:55"} */
    let str = e.currentTarget.dataset.v
    let val = str.schedule.split('-')
    str.startTime = val[0]
    str.endTime = val[1]
    wx.navigateTo({
      url: `/pagesDoctor/newScheduling/index?date=${JSON.stringify(str)}`,
    })
  },

  /*   formatter(e) {
      console.log(e)
    }, */


  handleSelect(e, val) {
    this.setData({
      date: e.detail ? this.formatDateToYYYYMMDD(e.detail) : val
    })
    ApiScheduleInfo({
      date: this.data.date
    }).then(res => {
      this.setData({
        scheduleInfoArr: res.data
      })
    })
    /* this.setData({
      data: this.formatDateToYYYYMMDD(e.detail)
    }) */
  },
  //新增排班的界面
  newScheduling() {
    wx.navigateTo({
      url: `/pagesDoctor/newScheduling/index?date=${JSON.stringify({date:this.data.date})}`,
    })
  },

  formatDateToYYYYMMDD(dateString) {
    let date = new Date(dateString);
    let year = date.getFullYear();
    let month = (date.getMonth() + 1).toString().padStart(2, '0'); // 月份是从 0 开始的，所以要加 1
    let day = date.getDate().toString().padStart(2, '0'); // 补充零确保是 2 位数
    return `${year}-${month}-${day}`;
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */


  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})